var chartDom = document.getElementById('xuri');
var myChart = echarts.init(chartDom);
window.onresize = myChart.resize; 
var option;

var colors = ['rgba(136,226,226)', '#FF7853', 'rgba(242,146,104)'];
var bgColor = 'rgb(0,21,46)';

var itemStyle = {
    star5: {
        color: colors[1]
    },
    star4: {
        color: colors[2]
    },
    star3: {
        color: colors[0]
    },
    star2: {
        color: colors[0]
    },
    star1: {
        color: colors[1]
    },
};

var data = [{
    name: '国补',
    itemStyle: {
        color: colors[1]
    },
    children: [{
        name: '2019年',
        children: [{
            name: '1☆',
            children: [ {
                name: '财政部'
            }]
        }, ]
    }, {
        name: '2020年',
        children: [{
            name: '5☆',
            children: [{
                name: '财政部、发改委'
            },{
                name:'发改委'},{
                name :'发改委、财政部'},{
                name :'财政部、国家能源局'
                },{
                name:'财政部'
                }]
        },]
    }]
},
{
    name: '产业园',
    itemStyle: {
        color: colors[2]
    },
    children: [{
        name: '2016年',
        children: [{
            name: '4☆',
            children: [ {
                name: '发改委、住建部'
            },{
                name:'住建部、发改委'},{
                name :'国务院'},{
                name :'发改委、住建部'
                }]
        }, ]
    },
    {
        name: '2017年',
        children: [{
            name: '1☆',
            children: [ {
                name: '发改委、住建部'
            }]
        }, ]
    }, 
    {
        name: '2019年',
        children: [{
            name: '1☆',
            children: [ {
                name: '国务院办公厅'
            }]
        }, ]
    }, 
   {
        name: '2021年',
        children: [{
            name: '1☆',
            children: [{
                name: '国务院'
            },]
        },]
    }]
},
{
    name: '环保',
    itemStyle: {
        color: colors[0]
    },
    children: [{
        name: '2016年',
        children: [{
            name: '2☆',
            children: [ {
                name: '发改委、住建部'
            },{
                name:'住建部、国家能源局'},]
        }, ]
    },
    {
        name: '2017年',
        children: [{
            name: '3☆',
            children: [ {
                name: '环保部'
            },{
                name: '环保部'
            },{
                name: '环保部'
            },
            ]
        }, ]
    }, 
    {
        name: '2018年',
        children: [{
            name: '2☆',
            children: [ {
                name: '环保部'
            }]
        }, ]
    }, 
    {
        name: '2020年',
        children: [{
            name: '2☆',
            children: [ {
                name: '财政部、生态环境部'
            },
            {
                name: '发改委'
            }]
        }, ]
    }, ]
},
];



for (var j = 0; j < data.length; ++j) {
    var level1 = data[j].children;
    for (var i = 0; i < level1.length; ++i) {
        var block = level1[i].children;
        var bookScore = [];
        var bookScoreId;
        for (var star = 0; star < block.length; ++star) {
            var style = (function (name) {
                switch (name) {
                case '5☆':
                    bookScoreId = 0;
                    return itemStyle.star5;
                case '4☆':
                    bookScoreId = 1;
                    return itemStyle.star4;
                case '3☆':
                    bookScoreId = 2;
                    return itemStyle.star3;
                case '2☆':
                    bookScoreId = 3;
                    return itemStyle.star2;
                 case '1☆':
                    bookScoreId = 3;
                    return itemStyle.star1;
                }
            })(block[star].name);

            block[star].label = {
                color: style.color,
                downplay: {
                    opacity: 0.5
                }
            };

            if (block[star].children) {
                style = {
                    opacity: 1,
                    color: style.color
                };
                block[star].children.forEach(function (book) {
                    book.value = 1;
                    book.itemStyle = style;

                    book.label = {
                        color: style.color
                    };

                    var value = 1;
                    if (bookScoreId === 0 || bookScoreId === 3) {
                        value = 5;
                    }

                    if (bookScore[bookScoreId]) {
                        bookScore[bookScoreId].value += value;
                    }
                    else {
                        bookScore[bookScoreId] = {
                            color: colors[bookScoreId],
                            value: value
                        };
                    }
                });
            }
        }

        level1[i].itemStyle = {
            color: data[j].itemStyle.color
        };
    }
}

option = {
         graphic: [
        {
           type:"image",
           left:"5%",
           bottom:"91.5%",
           style: {
               image:"https://i.loli.net/2021/06/21/CvlGW8PErboK2YQ.png",
               width:"68",
               height:"45",
            }
         },
         ],
    backgroundColor: bgColor,
    title: {
        text: '2016-2021年，国务院各部委发布的三类垃圾焚烧相关政策',
        left:'12%',
        top: '3%',
        textStyle: {
            fontSize:20,
            color:"#FFFFFF",
            fontFamily: 'Microsoft YaHei',
        },
    },
    color: colors,
    series: [{
        type: 'sunburst',
        center: ['47%', '57%'],
        data: data,
        sort: function (a, b) {
            if (a.depth === 1) {
                return b.getValue() - a.getValue();
            }
            else {
                return a.dataIndex - b.dataIndex;
            }
        },
        label: {
            rotate: 'radial',
            color: bgColor
        },
        itemStyle: {
            borderColor: bgColor,
            borderWidth: 2
        },
        levels: [{}, {
            r0: 0,
            r: 40,
            label: {
                rotate: 0
            }
        }, {
            r0: 40,
            r: 105
        }, {
            r0: 115,
            r: 140,
            itemStyle: {
                shadowBlur: 2,
                shadowColor: colors[2],
                color: 'transparent'
            },
            label: {
                rotate: 'tangential',
                fontSize: 10,
                color: colors[0]
            }
        }, {
            r0: 140,
            r: 145,
            itemStyle: {
                shadowBlur: 80,
                shadowColor: colors[0]
            },
            label: {
                position: 'outside',
                textShadowBlur: 5,
                textShadowColor: '#333'
            },
            downplay: {
                label: {
                    opacity: 0.5
                }
            }
        }]
    }],
};

option && myChart.setOption(option);
